<?php

class COrderAttachment extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	protected $is_seller='1';
	
	public function setIsSeller($is_seller){
		
		$this->is_seller=$is_seller;
		
	}
	
	public function _setup(){
		
		$this->_name=R_PRE."order_attachments";
		
		$this->_primary="attachmentid";
		
		parent::_setup();
		
	}
	
	/* $data=array(
					array('orderid'=>'1','attachment_url'=>'dfdsfsdf','ordering'=>'0','intro'=>'dfsdfsdf')
	*/
	public function createAttachments($data){

		$attachmentid=$this->insert($data);	
		
		return $attachmentid;
		
	}
	
	

	
	public function getAttachments($orderid){
		
		$db=$this->getAdapter();
		
		$where=$db->quoteInto(" orderid = ? ",$orderid)
				.' AND '.$db->quoteInto(" is_seller=? ",$this->is_seller);
		
		return $this->fetchAll($where)->toArray();
		
	}
	
	public function getAttachmentByAttachmentid($attachmentid){
		
		$db=$this->getAdapter();
		
		$where=$db->quoteInto(" attachmentid = ? ",$attachmentid);
		
		return $this->fetchRow($where)->toArray();		
		
	}
	
	
	public function delAttachmentsByItemid($orderidArray){
		
		$db=$this->getAdapter();
		
		$itemidString=join(',',$orderidArray);
		
		$where=$db->quoteInto('orderid IN (?)',$orderidArray);
		
		$thisArray=$this->fetchAll($where)->toArray();
		
		Zend_Loader::loadClass("CPath");
		
		for($i=0;$i<count($thisArray);$i++){
			
			unlink(CPath::toAbsoluteRootPath($thisArray[$i]['attachment_url']));
			
			unlink(CPath::toAbsoluteRootPath($thisArray[$i]['cut_url']));
			
		}
		
		$result=$db->query("DELETE FROM $this->_name WHERE itemid IN ($itemidString)");
		
		return $result;
		
	}
	
	public function delAttachmentsById($attachmentIdArray){
		
		$db=$this->getAdapter();
		
		$attachmentIdString=join(',',$attachmentIdArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE attachmentid IN ($attachmentIdString)");
		
		return $result;		
		
	}

	
}